package gov.dhs.cis.soa.xslt;

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.ErrorListener;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.w3c.dom.Document;

public class ReportUML implements ErrorListener {
	static final Logger loger = Logger.getLogger(ReportUML.class);
	
	public void transform(String inXML, String inXSL, String outTXT)
			throws TransformerConfigurationException, TransformerException {

		TransformerFactory factory = TransformerFactory.newInstance();
		
		InputStream inXSLStream = this.getClass().getClassLoader().getResourceAsStream(Configuration.xslFolder + inXSL);
		StreamSource xslStream = new StreamSource(inXSLStream);
		
		Transformer transformer = factory.newTransformer(xslStream);
		transformer.setErrorListener(this);
		//to indent
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
		
		InputStream inXMLStream = this.getClass().getClassLoader().getResourceAsStream(inXML);
		StreamSource in = new StreamSource(inXMLStream);
		
		StreamResult out = new StreamResult(Configuration.outFolder + outTXT);
		System.out.println("Extracting from : " + inXSL);
		transformer.transform(in, out);
		System.out.println("The generated TEXT file is:" + outTXT);
	}

	public void error(TransformerException e) throws TransformerException {
		show("Warning", e);
		throw (e);
	}

	public void fatalError(TransformerException e) throws TransformerException {
		show("Error", e);
		throw (e);
	}

	public void warning(TransformerException e) throws TransformerException {
		show("Warning", e);
		throw (e);
	}

	private void show(String type, TransformerException e) {
		System.out.println(type + ": " + e.getMessage());
		if (e.getLocationAsString() != null)
			System.out.println(e.getLocationAsString());
	}
	
	public void initLog4J() {
		try {
			String fileName = "log4j.xml";
			InputStream stream = this.getClass().getClassLoader().getResourceAsStream(fileName);
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			docFactory.setValidating(false);
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(stream);
	        DOMConfigurator conf = new DOMConfigurator();
	        conf.doConfigure(doc.getDocumentElement(), LogManager.getLoggerRepository());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ReportUML rpt = new ReportUML();
		String modelXMIFIle = "USCISIOEModel1.uml";
		//String[] xslFiles = { "services", "servicesops" };
		String[] xslFiles = { "wmb_services" };

		try {
			rpt.initLog4J();
			
			for(String xslFile : xslFiles) {
				rpt.transform(modelXMIFIle, xslFile + ".xsl", xslFile + ".xml");
				System.out.println(xslFile + " were extracted");
				//rpt.transform2XML(xslFile + ".txt", null);
			}
		} catch (Exception e) {
			loger.error(e);
			e.printStackTrace();
		}
	}

}
